/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ajz.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ajz.entity.AjzModel;
import com.jeesite.modules.ajz.entity.AjzModelContent;
import com.jeesite.modules.ajz.service.AjzModelContentService;
import com.jeesite.modules.ajz.service.AjzModelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 模板内容Controller
 * @author cww
 * @version 2019-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/ajz/ajzModelContent")
public class AjzModelContentController extends BaseController {

	@Autowired
	private AjzModelContentService ajzModelContentService;
	@Autowired
	private AjzModelService ajzModelService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AjzModelContent get(String id, boolean isNewRecord) {
		return ajzModelContentService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ajz:ajzModelContent:view")
	@RequestMapping(value = {"list", ""})
	public String list(AjzModelContent ajzModelContent,  HttpServletRequest request, HttpServletResponse response,Model model) {
		String model_id = request.getParameter("model_id");
		AjzModel ajzModel = ajzModelService.get(model_id);
		ajzModelContent.setAjzModel(ajzModel);

		model.addAttribute("ajzModelContent", ajzModelContent);
		return "modules/ajz/ajzModelContentList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ajz:ajzModelContent:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AjzModelContent> listData(AjzModelContent ajzModelContent, HttpServletRequest request, HttpServletResponse response) {
		String modelId = ajzModelContent.getAjzModel().getId();
		ajzModelContent.setPage(new Page<>(request, response));
//		//通过 某个字段 获取带有id的对象
//		AjzModel ajzModel = ajzModelContent.getAjzModel();
//		if (ajzModel != null && !StringUtils.isEmpty(ajzModel.getBaseType())) {
//			ajzModel.setPage(new Page<>(request, response));
//			ajzModel = ajzModelService.findPage(ajzModelContent.getAjzModel()).getList().get(0);
//		}else{
//			ajzModel = new AjzModel();
//		}
//		ajzModelContent.setAjzModel(ajzModel);
		ajzModelContent.setModelId(modelId);
		Page<AjzModelContent> page = ajzModelContentService.findPage(ajzModelContent);

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ajz:ajzModelContent:view")
	@RequestMapping(value = "form")
	public String form(AjzModelContent ajzModelContent, Model model, HttpServletRequest request, HttpServletResponse response) {
		String modelId = request.getParameter("modelId");
		ajzModelContent.setAjzModel(ajzModelService.get(modelId));
		model.addAttribute("ajzModelContent", ajzModelContent);
		return "modules/ajz/ajzModelContentForm";
	}

	/**
	 * 保存模板内容
	 */
	@RequiresPermissions("ajz:ajzModelContent:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AjzModelContent ajzModelContent,HttpServletRequest request, HttpServletResponse response) {
		String modelId = request.getParameter("modelId");
		ajzModelContent.setModelId(modelId);
		ajzModelContentService.save(ajzModelContent);
		return renderResult(Global.TRUE, text("保存模板内容成功！"));
	}
	
	/**
	 * 删除模板内容
	 */
	@RequiresPermissions("ajz:ajzModelContent:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AjzModelContent ajzModelContent) {
		ajzModelContentService.delete(ajzModelContent);
		return renderResult(Global.TRUE, text("删除模板内容成功！"));
	}
	
}